package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.NursingLevel;
import com.itheima.domain.NursingProject;
import com.itheima.domain.PageResult;
import com.itheima.domain.Result;
import com.itheima.domain.query.NursingLevelQueryParam;
import com.itheima.domain.query.NursingProjectQueryParam;
import com.itheima.service.NursingLevelService;
import com.itheima.service.NursingProjectService;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/nursingLevel")
@RequiredArgsConstructor
public class NursingLevelController {
    private final NursingLevelService nursingLevelService;

    @PostMapping("/page")
    public Result page(@RequestBody NursingLevelQueryParam nursingLevelQueryParam){
        //获取并封装分页数据
        Page<NursingLevel> page = new Page<>(nursingLevelQueryParam.getCurrentPage(),nursingLevelQueryParam.getPageSize());

        // 构建条件查询
        String name = nursingLevelQueryParam.getName();
        Integer status = nursingLevelQueryParam.getStatus();

        LambdaQueryWrapper<NursingLevel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != "",NursingLevel::getName,name)
                .eq(status != null,NursingLevel::getStatus,status);


        page = nursingLevelService.page(page,queryWrapper);

        return new Result(200,new PageResult(1,page.getRecords(),(int) page.getTotal()),"查询成功");
    }


    @PostMapping("/add")
    public Result add(@RequestBody NursingLevel nursingLevel){
        nursingLevel.setCreateTime(LocalDateTime.now());
        nursingLevel.setUpdateTime(LocalDateTime.now());
        boolean success = nursingLevelService.save(nursingLevel);
        Result result = new Result(0,null,"添加失败");
        if (success) {
            result.setCode(200);
            result.setMsg("添加成功");
        }
        return result;
    }


    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer id) {
        NursingLevel nl = nursingLevelService.getById(id);
        Result result = new Result(0,null,"ID查询失败,无该护理项目");
        if (nl != null) {
            result.setCode(200);
            result.setData(nl);
            result.setMsg("查询成功");
        }
        return result;
    }

    @PutMapping("/update")
    public Result update(@RequestBody NursingLevel nursingLevel){
        nursingLevel.setUpdateTime(LocalDateTime.now());
        boolean success = nursingLevelService.updateById(nursingLevel);
        Result result = new Result(0,null,"修改失败");
        if (success) {
            result.setCode(200);
            result.setMsg("修改成功");
        }
        return result;
    }

    @DeleteMapping("/delete/{id}")
    public Result deleteByIds(@PathVariable("id") Integer id){
        boolean success = nursingLevelService.removeById(id);
        Result result = new Result(0,null,"删除失败");
        if (success) {
            result.setCode(200);
            result.setMsg("删除成功");
        }
        return result;
    }
}
